package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.DeviceMapper;
import com.lucher.mes.dao.DeviceinoutMapper;
import com.lucher.mes.po.Deviceinout;
import com.lucher.mes.service.IOperatorService;

/**
 * 用户控制层
 */
@Controller
@Scope("prototype")
@RequestMapping("/deviceinout")
@Transactional
public class DeviceinoutController extends BaseController {

	@Autowired
	private DeviceinoutMapper deviceinoutDao;
	@Autowired
	private IOperatorService operatorService;
	@Autowired
	private DeviceMapper deviceDao;
	
	
	/**
	 * 查询材料库存
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Deviceinout form) {
		return "deviceinout/findDeviceinoutList";
	}
	
	/**
	 * 查询材料库存Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Deviceinout form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		if(StringUtils.isNotBlank(form.getRangeDate())){
			String[] datsArray = form.getRangeDate().split(" - ");
			if(datsArray!=null && datsArray.length == 2){
				form.setBegintime(datsArray[0]);
				form.setEndtime(datsArray[1]);
			}
		}
		
		Integer total = deviceinoutDao.findByCount(form);
		List<Deviceinout> list = deviceinoutDao.findByListForPage(form);
		for (Deviceinout deviceinout : list) {
			
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			objectMap.put("id", deviceinout.getId());
			objectMap.put("devicecode", deviceinout.getDevicecode());
			objectMap.put("devicename", deviceinout.getDevicename());
			objectMap.put("operatorcode", deviceinout.getOperatorcode());
			objectMap.put("batchno", deviceinout.getBatchno());
			objectMap.put("type", deviceinout.getType());
			objectMap.put("typeinfo", deviceinout.getTypeinfo());
			objectMap.put("addtime", StringUtils.isEmpty(deviceinout.getAddtime())?"":Tools.getStr(deviceinout.getAddtime()).substring(0, 19));
			objectMap.put("operatorcode", deviceinout.getOperatorcode());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);//页面总数
		result.put("data", objectlist);
		return result;
	}
}
